package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameInfoDetialV2Bean;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.response.GreatCrackCollectionResponse;
import cn.wit.summit.chwan.models.response.YesterdayIncomeRankingResponse;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.ItemListViewV2;

/**
 * Created by Alan on 2018/7/19.
 */

public class MakeMoneyRankingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;

    public MakeMoneyRankingAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public enum ViewType {
        TYPE_ITEM_MY,
        TYPE_ITEM
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_ITEM_MY.ordinal()) {
            holder = new ItemHolder(inflater.inflate(R.layout.item_makemoney_ranking_view, parent, false));
        } else if (viewType == ViewType.TYPE_ITEM.ordinal()) {
            holder = new ItemHolder(inflater.inflate(R.layout.item_makemoney_ranking_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (viewBean.getViewType().ordinal() == ViewType.TYPE_ITEM_MY.ordinal()) {
            final YesterdayIncomeRankingResponse.MyRankBean temp = (YesterdayIncomeRankingResponse.MyRankBean) viewBean.getObj();
            ItemHolder itemHolder = (ItemHolder) holder;

            itemHolder.itemMakeMoneyRankingRootRl.setBackgroundResource(R.drawable.item_root_rank_bg);
            itemHolder.itemMakeMoneyRankingIv.setVisibility(View.GONE);
            itemHolder.itemMakeMoneyRankingNumTv.setVisibility(View.GONE);
            itemHolder.itemMakeMoneyRankingTv.setVisibility(View.VISIBLE);
            itemHolder.itemMakeMoneyRankingTv.setText("第 "+temp.getRank()+" 名");
            itemHolder.itemMakeMoneyRankingLine.setVisibility(View.GONE);
            itemHolder.itemMakeMoneyRankingNameTv.setText(temp.getNickname());
            itemHolder.itemMakeMoneyRankingIncomeTv.setText(temp.getScore()+"");
            itemHolder.itemMakeMoneySoreTv.setText("信用分："+ temp.getCredit() +" | ");

            String bgUrl = temp.getAvatar();
            if (!StringUtils.isEmpty(bgUrl))
                itemHolder.itemMakeMoneyRankingHeaderIv.setImageURI(bgUrl);
        } else if (viewBean.getViewType().ordinal() == ViewType.TYPE_ITEM.ordinal()) {
            final YesterdayIncomeRankingResponse.MyRankBean temp = (YesterdayIncomeRankingResponse.MyRankBean) viewBean.getObj();
            ItemHolder itemHolder = (ItemHolder) holder;

            itemHolder.itemMakeMoneySoreTv.setText("信用分："+ temp.getCredit() +" | ");
            itemHolder.itemMakeMoneyRankingRootRl.setBackgroundResource(R.drawable.item_root_rank_white_bg);
            itemHolder.itemMakeMoneyRankingTv.setVisibility(View.GONE);
            if (temp.isLast()) {
                itemHolder.itemMakeMoneyRankingLine.setVisibility(View.GONE);
            } else {
                itemHolder.itemMakeMoneyRankingLine.setVisibility(View.VISIBLE);
            }
            if (temp.getPosition()==0) {
                itemHolder.itemMakeMoneyRankingIv.setVisibility(View.VISIBLE);
                itemHolder.itemMakeMoneyRankingNumTv.setVisibility(View.GONE);
                itemHolder.itemMakeMoneyRankingIv.setImageResource(R.mipmap.ic_income_ranking_one);
            } else if (temp.getPosition()==1) {
                itemHolder.itemMakeMoneyRankingIv.setVisibility(View.VISIBLE);
                itemHolder.itemMakeMoneyRankingNumTv.setVisibility(View.GONE);
                itemHolder.itemMakeMoneyRankingIv.setImageResource(R.mipmap.ic_income_ranking_two);
            } else if (temp.getPosition()==2) {
                itemHolder.itemMakeMoneyRankingIv.setVisibility(View.VISIBLE);
                itemHolder.itemMakeMoneyRankingNumTv.setVisibility(View.GONE);
                itemHolder.itemMakeMoneyRankingIv.setImageResource(R.mipmap.ic_income_ranking_three);
            } else {
                itemHolder.itemMakeMoneyRankingIv.setVisibility(View.GONE);
                itemHolder.itemMakeMoneyRankingNumTv.setVisibility(View.VISIBLE);
                itemHolder.itemMakeMoneyRankingNumTv.setText((temp.getPosition()+1)+"");
            }
            itemHolder.itemMakeMoneyRankingNameTv.setText(temp.getNickname());
            itemHolder.itemMakeMoneyRankingIncomeTv.setText(temp.getScore()+"");

            String bgUrl = temp.getAvatar();
            if (!StringUtils.isEmpty(bgUrl))
                itemHolder.itemMakeMoneyRankingHeaderIv.setImageURI(bgUrl);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemMakeMoneyRankingRootRl;
        ImageView itemMakeMoneyRankingIv;
        SimpleDraweeView itemMakeMoneyRankingHeaderIv;
        TextView itemMakeMoneyRankingNameTv;
        TextView itemMakeMoneyRankingTv;
        TextView itemMakeMoneyRankingIncomeTv;
        TextView itemMakeMoneyRankingNumTv;
        TextView itemMakeMoneySoreTv;
        View itemMakeMoneyRankingLine;

        public ItemHolder(View view) {
            super(view);
            itemMakeMoneyRankingRootRl = view.findViewById(R.id.itemMakeMoneyRankingRootRl);
            itemMakeMoneyRankingIv = view.findViewById(R.id.itemMakeMoneyRankingIv);
            itemMakeMoneyRankingHeaderIv = view.findViewById(R.id.itemMakeMoneyRankingHeaderIv);
            itemMakeMoneyRankingNameTv = view.findViewById(R.id.itemMakeMoneyRankingNameTv);
            itemMakeMoneyRankingTv = view.findViewById(R.id.itemMakeMoneyRankingTv);
            itemMakeMoneyRankingIncomeTv = view.findViewById(R.id.itemMakeMoneyRankingIncomeTv);
            itemMakeMoneyRankingNumTv = view.findViewById(R.id.itemMakeMoneyRankingNumTv);
            itemMakeMoneySoreTv = view.findViewById(R.id.itemMakeMoneySoreTv);
            itemMakeMoneyRankingLine = view.findViewById(R.id.itemMakeMoneyRankingLine);
        }
    }
}
